package com.ews.onlineexchange.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Named;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.Trade;


public class TxtUtil implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void writeFile(Bulletin bulletin, List<Trade> orginalList, List<Trade> sortedList) {
		try {
			StringBuilder fileNameSb = new StringBuilder(bulletin.getProjectName());
			fileNameSb.append("(");
			fileNameSb.append(bulletin.getSerialNumber());
			fileNameSb.append("");
			File file = new File(fileNameSb.toString()); // 相对路径，如果没有则要建立一个新的output.txt文件
			file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
			FileWriter writer = new FileWriter(file); 
			BufferedWriter out = new BufferedWriter(writer);
			StringBuilder orginalStr = new StringBuilder("排序前：");
			StringBuilder sortedStr = new StringBuilder();
			
			out.write("我会写入文件啦1\r\n"); // \r\n即为换行
			out.write("我会写入文件啦2\r\n"); // \r\n即为换行
			out.flush(); // 把缓存区内容压入文件
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getStrFromTradeList(List<Trade> tradeList) {
		StringBuilder sb = new StringBuilder();
		for (Trade trade : tradeList) {
			sb.append(trade.getSupplier().getCnname());
			sb.append(";");	
		}
		if(sb.length() > 0) return sb.substring(0, sb.length()-1);
		else return sb.toString();
	}
}
